<script setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// 个人信息数据
const userInfo = ref({
  name: '陈老师',
  email: 'chen@example.com',
  phone: '13800138000',
  department: '心理健康中心',
  position: '心理咨询师',
  avatar: ''
})

// 编辑弹窗状态
const editDialogVisible = ref(false)
const editForm = ref({...userInfo.value})

// 修改个人信息
const editUserInfo = () => {
  editForm.value = {...userInfo.value}
  editDialogVisible.value = true
}

// 保存编辑
const saveEdit = () => {
  userInfo.value = {...editForm.value}
  editDialogVisible.value = false
  ElMessage.success('修改成功')
}

// 取消编辑
const cancelEdit = () => {
  editDialogVisible.value = false
}

// 头像上传处理
const handleAvatarChange = (file) => {
  const reader = new FileReader()
  reader.onload = (e) => {
    editForm.value.avatar = e.target.result
  }
  reader.readAsDataURL(file.raw)
}

// 修改密码弹窗状态
const passwordDialogVisible = ref(false)
const passwordForm = ref({
  oldPasswordOrIdCard: '',
  newPassword: '',
  confirmPassword: ''
})

// 修改密码
const changePassword = () => {
  passwordDialogVisible.value = true
}

// 保存密码修改
const savePasswordChange = () => {
  // 验证输入
  if (!passwordForm.value.oldPasswordOrIdCard) {
    ElMessage.warning('请输入旧密码或身份证后6位')
    return
  }
  if (!passwordForm.value.newPassword) {
    ElMessage.warning('请输入新密码')
    return
  }
  if (passwordForm.value.newPassword.length < 6) {
    ElMessage.warning('新密码长度不能少于6位')
    return
  }
  if (passwordForm.value.newPassword !== passwordForm.value.confirmPassword) {
    ElMessage.warning('新密码与确认密码不一致')
    return
  }
  
  // 这里可以添加实际的密码修改逻辑
  console.log('密码修改成功', passwordForm.value)
  
  ElMessage.success('密码修改成功！')
  passwordDialogVisible.value = false
  
  // 重置表单
  passwordForm.value = {
    oldPasswordOrIdCard: '',
    newPassword: '',
    confirmPassword: ''
  }
}

// 取消密码修改
const cancelPasswordChange = () => {
  passwordDialogVisible.value = false
  // 重置表单
  passwordForm.value = {
    oldPasswordOrIdCard: '',
    newPassword: '',
    confirmPassword: ''
  }
}
</script>

<template>
  <div class="personal-center">
    <div class="header">
      <h2>个人中心</h2>
    </div>

    <div class="content">
      <!-- 个人信息卡片 -->
      <div class="info-card">
        <div class="avatar-section">
          <div class="avatar">
            <img v-if="userInfo.avatar" :src="userInfo.avatar" class="avatar-img" />
            <el-icon v-else size="60"><User /></el-icon>
          </div>
          <div class="user-basic">
            <h3>{{ userInfo.name }}</h3>
            <p>{{ userInfo.position }}</p>
          </div>
        </div>

        <div class="info-details">
          <div class="info-item">
            <label>姓名：</label>
            <span>{{ userInfo.name }}</span>
          </div>
          <div class="info-item">
            <label>邮箱：</label>
            <span>{{ userInfo.email }}</span>
          </div>
          <div class="info-item">
            <label>电话：</label>
            <span>{{ userInfo.phone }}</span>
          </div>
          <div class="info-item">
            <label>部门：</label>
            <span>{{ userInfo.department }}</span>
          </div>
          <div class="info-item">
            <label>职位：</label>
            <span>{{ userInfo.position }}</span>
          </div>
        </div>

        <div class="actions">
          <el-button type="primary" @click="editUserInfo">编辑信息</el-button>
          <el-button @click="changePassword">修改密码</el-button>
        </div>
      </div>
    </div>
  </div>

  <!-- 编辑信息弹窗 -->
  <el-dialog
    v-model="editDialogVisible"
    title="编辑个人信息"
    width="500px"
    :before-close="cancelEdit"
  >
    <el-form :model="editForm" label-width="80px">
      <!-- 头像上传 -->
      <el-form-item label="头像">
        <el-upload
          class="avatar-uploader"
          action="#"
          :show-file-list="false"
          :on-change="handleAvatarChange"
          :auto-upload="false"
        >
          <img v-if="editForm.avatar" :src="editForm.avatar" class="avatar-preview" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>

      <el-form-item label="姓名">
        <el-input v-model="editForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="editForm.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="editForm.phone" placeholder="请输入电话"></el-input>
      </el-form-item>
      <el-form-item label="部门">
        <el-input v-model="editForm.department" placeholder="请输入部门"></el-input>
      </el-form-item>
      <el-form-item label="职位">
        <el-input v-model="editForm.position" placeholder="请输入职位"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancelEdit">取消</el-button>
        <el-button type="primary" @click="saveEdit">保存</el-button>
      </span>
    </template>
  </el-dialog>
  
  <!-- 修改密码弹窗 -->
  <el-dialog
    v-model="passwordDialogVisible"
    title="修改密码"
    width="500px"
    :before-close="cancelPasswordChange"
  >
    <el-form :model="passwordForm" label-width="120px">
      <el-form-item label="旧密码/身份证">
        <el-input 
          v-model="passwordForm.oldPasswordOrIdCard" 
          placeholder="请输入旧密码或身份证后6位"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码">
        <el-input 
          v-model="passwordForm.newPassword" 
          placeholder="请输入新密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input 
          v-model="passwordForm.confirmPassword" 
          placeholder="请再次输入新密码"
          show-password
        ></el-input>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancelPasswordChange">取消</el-button>
        <el-button type="primary" @click="savePasswordChange">确认修改</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped>
.personal-center {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 570px;
}

.header {
  margin-bottom: 20px;
}

.header h2 {
  color: #333;
  font-size: 24px;
  font-weight: 500;
}

.content {
  max-width: 800px;
}

.info-card {
  background: white;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.avatar-section {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background-color: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
  color: #999;
}

.user-basic h3 {
  margin: 0 0 5px 0;
  font-size: 20px;
  color: #333;
}

.user-basic p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.info-details {
  margin-bottom: 30px;
}

.info-item {
  display: flex;
  margin-bottom: 15px;
  align-items: center;
}

.info-item label {
  width: 80px;
  color: #666;
  font-weight: 500;
}

.info-item span {
  color: #333;
}

.actions {
  display: flex;
  gap: 10px;
}

/* 头像上传样式 */
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100px;
  height: 100px;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar-img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
}

.avatar-preview {
  width: 100px;
  height: 100px;
  display: block;
  border-radius: 6px;
  object-fit: cover;
}
</style>
